# -*- coding: utf-8 -*-
import codecs
import csv
import json

import pymysql


class LoadDatToCsv(object):

    def __init__(self):
        self.connection = pymysql.connect(host='localhost',
                                          user='root',
                                          password='123456',
                                          db='hearthstonestation',
                                          charset='utf8mb4',
                                          cursorclass=pymysql.cursors.DictCursor)
        self.cursor = self.connection.cursor()

    def read_data(self):

        with codecs.open(filename='./cards.json', mode='w', encoding='utf-8') as f:

            sql = 'select * from cards_hscards'
            self.cursor.execute(sql)
            data = self.cursor.fetchall()
            data = {'data': data}
            f.write(json.dumps(data))
            # for res in data:
            #     print(res)
            #     f.write(json.dumps(res))

    def save_to_csv(self, data):
        pass


if __name__ == '__main__':
    load = LoadDatToCsv()
    load.read_data()
